// index.js
Page({
  data: {
    cityList: ['上海', '北京', '广州', '深圳', '杭州', '成都', '南京'],
    selectedCity: '上海',
    weatherList: [
      { date: '9/26', icon: '/images/weather/sunny.png', desc: '晴', temp: '25°C' },
      { date: '9/27', icon: '/images/weather/cloudy.png', desc: '多云', temp: '24°C' },
      { date: '9/28', icon: '/images/weather/rain.png', desc: '小雨', temp: '22°C' },
      { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '26°C' },
      { date: '9/30', icon: '/images/weather/cloudy.png', desc: '多云', temp: '23°C' },
      { date: '10/1', icon: '/images/weather/thunder.png', desc: '雷阵雨', temp: '21°C' },
      { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '27°C' },
    ],
    activities: [
      {
        id: 1,
        name: "网球陪练4.0",
        date: "5月15日 07:00-23:22",
        location: "静安寺 距您1km",
        participants: "已报名3人 | 差2人",
        price: "￥120.00",
        status: "报名中"
      },
      {
        id: 2,
        name: "跃网·Rising大华室内网球畅打活动",
        date: "5月15日 10:00-19:00",
        location: "大华 距您3.1km",
        participants: "已报名5人 | 差3人",
        price: "￥150.00",
        status: "报名中"
      }
    ]
  },

  onCityChange: function(e) {
    const idx = e.detail.value;
    const city = this.data.cityList[idx];
    // 不同城市天气数据
    const weatherMap = {
      '上海': [
        { date: '9/26', icon: '/images/weather/sunny.png', desc: '晴', temp: '25°C' },
        { date: '9/27', icon: '/images/weather/cloudy.png', desc: '多云', temp: '24°C' },
        { date: '9/28', icon: '/images/weather/rain.png', desc: '小雨', temp: '22°C' },
        { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '26°C' },
        { date: '9/30', icon: '/images/weather/cloudy.png', desc: '多云', temp: '23°C' },
        { date: '10/1', icon: '/images/weather/thunder.png', desc: '雷阵雨', temp: '21°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '27°C' },
      ],
      '北京': [
        { date: '9/26', icon: '/images/weather/cloudy.png', desc: '多云', temp: '20°C' },
        { date: '9/27', icon: '/images/weather/sunny.png', desc: '晴', temp: '22°C' },
        { date: '9/28', icon: '/images/weather/rain.png', desc: '小雨', temp: '19°C' },
        { date: '9/29', icon: '/images/weather/cloudy.png', desc: '多云', temp: '21°C' },
        { date: '9/30', icon: '/images/weather/sunny.png', desc: '晴', temp: '23°C' },
        { date: '10/1', icon: '/images/weather/rain.png', desc: '小雨', temp: '18°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '24°C' },
      ],
      '广州': [
        { date: '9/26', icon: '/images/weather/rain.png', desc: '阵雨', temp: '28°C' },
        { date: '9/27', icon: '/images/weather/cloudy.png', desc: '多云', temp: '29°C' },
        { date: '9/28', icon: '/images/weather/sunny.png', desc: '晴', temp: '30°C' },
        { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '31°C' },
        { date: '9/30', icon: '/images/weather/rain.png', desc: '阵雨', temp: '27°C' },
        { date: '10/1', icon: '/images/weather/cloudy.png', desc: '多云', temp: '28°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '32°C' },
      ],
      '深圳': [
        { date: '9/26', icon: '/images/weather/cloudy.png', desc: '多云', temp: '27°C' },
        { date: '9/27', icon: '/images/weather/rain.png', desc: '小雨', temp: '26°C' },
        { date: '9/28', icon: '/images/weather/sunny.png', desc: '晴', temp: '28°C' },
        { date: '9/29', icon: '/images/weather/cloudy.png', desc: '多云', temp: '27°C' },
        { date: '9/30', icon: '/images/weather/sunny.png', desc: '晴', temp: '29°C' },
        { date: '10/1', icon: '/images/weather/rain.png', desc: '小雨', temp: '26°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '30°C' },
      ],
      '杭州': [
        { date: '9/26', icon: '/images/weather/sunny.png', desc: '晴', temp: '23°C' },
        { date: '9/27', icon: '/images/weather/cloudy.png', desc: '多云', temp: '22°C' },
        { date: '9/28', icon: '/images/weather/rain.png', desc: '小雨', temp: '21°C' },
        { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '24°C' },
        { date: '9/30', icon: '/images/weather/cloudy.png', desc: '多云', temp: '23°C' },
        { date: '10/1', icon: '/images/weather/rain.png', desc: '小雨', temp: '20°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '25°C' },
      ],
      '成都': [
        { date: '9/26', icon: '/images/weather/cloudy.png', desc: '多云', temp: '19°C' },
        { date: '9/27', icon: '/images/weather/rain.png', desc: '小雨', temp: '18°C' },
        { date: '9/28', icon: '/images/weather/cloudy.png', desc: '多云', temp: '20°C' },
        { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '21°C' },
        { date: '9/30', icon: '/images/weather/cloudy.png', desc: '多云', temp: '19°C' },
        { date: '10/1', icon: '/images/weather/rain.png', desc: '小雨', temp: '18°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '22°C' },
      ],
      '南京': [
        { date: '9/26', icon: '/images/weather/sunny.png', desc: '晴', temp: '24°C' },
        { date: '9/27', icon: '/images/weather/cloudy.png', desc: '多云', temp: '23°C' },
        { date: '9/28', icon: '/images/weather/rain.png', desc: '小雨', temp: '22°C' },
        { date: '9/29', icon: '/images/weather/sunny.png', desc: '晴', temp: '25°C' },
        { date: '9/30', icon: '/images/weather/cloudy.png', desc: '多云', temp: '24°C' },
        { date: '10/1', icon: '/images/weather/rain.png', desc: '小雨', temp: '21°C' },
        { date: '10/2', icon: '/images/weather/sunny.png', desc: '晴', temp: '26°C' },
      ]
    };
    this.setData({
      selectedCity: city,
      weatherList: weatherMap[city] || []
    });
  },
       
  onJoinActivity: function(e) {
    const isLoggedIn = wx.getStorageSync('isLoggedIn');
    if (!isLoggedIn) {
      wx.setStorageSync('redirectUrl', '/pages/index/index'); // 保存当前页面 URL
      wx.showModal({
        title: '未登录',
        content: '请先登录后再参与活动。',
        showCancel: false,
        success: () => {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      });
      return;
    }

    // 获取当前活动对象
    const activity = e.currentTarget.dataset.activity;
    // 读取已报名列表
    let joined = wx.getStorageSync('joinedActivities') || [];
    // 检查是否已报名
    if (!joined.find(a => a.id === activity.id)) {
      joined.push(activity);
      wx.setStorageSync('joinedActivities', joined);
      wx.showToast({ title: '报名成功', icon: 'success' });
    } else {
      wx.showToast({ title: '已报名', icon: 'none' });
    }
  },

  onLoad: function() {
    // 页面初始化
    console.log("index is render")
    console.log(wx.canIUse('navigateTo'))
  },

  onShow: function() {
    // 每次页面显示时检查本地缓存是否有新活动
    const newActivity = wx.getStorageSync('newActivity');
    if (newActivity && newActivity.id) {
      // 检查是否已存在同 id 活动，避免重复添加
      const exists = this.data.activities.some(act => act.id === newActivity.id);
      if (!exists) {
        this.setData({
          activities: [...this.data.activities, newActivity]
        });
      }
      wx.removeStorageSync('newActivity');
    }

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().init && this.getTabBar().init();
    }
  },

});